package com.itheima.reggie.web.controller;


import com.itheima.reggie.domain.Category;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//管理端分类的表现层控制器
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询分类列表
    @GetMapping("/findAll")
    public ResultInfo findAll() {
        //调用service查询
        return ResultInfo.success(categoryService.findAll());
    }

    //新增分类
    @PostMapping
    public ResultInfo save(@RequestBody Category category) {
        //调用service
        categoryService.save(category);
        //返回成功
        return ResultInfo.success();
    }

    //修改分类
    @PutMapping
    public ResultInfo update(@RequestBody Category category) {
        //调用service
        categoryService.update(category);
        //返回成功
        return ResultInfo.success();
    }

    //删除分类
    @DeleteMapping
    public ResultInfo delete(Long id) {
        //调用service,返回结果
        ResultInfo resultInfo = categoryService.delete(id);
        return resultInfo;
    }


    //新增菜品之回显分类
    @GetMapping("/list")
    public ResultInfo findList(Integer type) {
        //调用service
        List<Category> categoryList = categoryService.findList(type);
        //返回
        return ResultInfo.success(categoryList);
    }
}